package com.yanqu.road.server.cmd.activity.douluoshrek;

import com.yanqu.road.pb.activity.DouLuoShrekProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shrek.DouLuoShrekModule;
import com.yanqu.road.server.manger.activity.shrek.DouLuoShrekMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = Protocol.S_DOULUOSHREK_MANUAL, desc = "")
public class ShrekGetManualCmd extends AbstractCommand {

    private int activityId;

    private Map<Integer,Integer> titleMap = new ConcurrentHashMap<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DouLuoShrekProto.ManualReqMsg reqMsg = DouLuoShrekProto.ManualReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(DouLuoShrekMgr.getActivityTimeAcId() != activityId){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        titleMap = player.getModule(DouLuoShrekModule.class).getTitleManualMap();
    }

    @Override
    public void sendData(GamePlayer player) {
        DouLuoShrekProto.ManualRespMsg.Builder respMsg = DouLuoShrekProto.ManualRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(ret == 0){
            for(int titleId : titleMap.keySet()){
                DouLuoShrekProto.ManualTemp.Builder manualTempMsg = DouLuoShrekProto.ManualTemp.newBuilder();
                manualTempMsg.setTitleId(titleId);
                manualTempMsg.setCount(titleMap.get(titleId));
                respMsg.addManuals(manualTempMsg);
            }
        }
        player.sendPacket(Protocol.U_DOULUOSHREK_MANUAL, respMsg);
    }
}
